layui.define(['jquery', 'form'], function (exports) {

    var $ = layui.jquery,
        form = layui.form;

    var formRender = {
        init: function (target, options, longLable) {
            // $(target).empty();

            // var targetId = $(target).attr('id');

            var targetHtml = '';
            var count = 0;
            for (var category in options) {
                targetHtml += '<fieldset class="layui-elem-field layui-field-title" ' + ((count++) && 'style="margin-top: 50px"') + '><legend ' + (longLable ? 'style="margin-left: 150px !important;"' : '') + '>[' + category + ']</legend></fieldset>';
                for (var key in options[category]) {
                    var ele = options[category][key];
                    targetHtml += '<div class="layui-form-item">\
                    <label class="layui-form-label" ' + (longLable ? 'style="width: 270px !important;"' : '') + ' title="' + (ele.zh || key) + '">' + (ele.zh || key) + '</label>\
                    <div class="layui-input-block" ' + (longLable ? 'style="margin-left: 300px !important;"' : '') + '>';

                    switch(ele.type) {
                        case 'text': targetHtml += this._text(ele, category + '////==////' + key); break;
                        case 'combobox': targetHtml += this._combobox(ele, category + '////==////' + key); break;
                        case 'numberbox': targetHtml += this._numberbox(ele, category + '////==////' + key); break;
                        default: break;
                    }

                    targetHtml += '</div></div>'
                }
            }
            $(target).prepend(targetHtml);
            form.render();
        },
        _combobox: function (ele, id) {
            var html = '<select name="' + id + '" id="' + id + '" ' + ((ele.readonly || '') && 'readonly') + '>';
            var optionArr = [];
            for (var i = 0; i < ele.data.length; i++) {
                if (ele.default == ele.data[i].id) {
                    optionArr.push('<option value="' + ele.data[i].id + '" selected="selected">' + ele.data[i].text + '</option>');
                } else {
                    optionArr.push('<option value="' + ele.data[i].id + '">' + ele.data[i].text + '</option>');
                }
            }
            html += optionArr.join('');
            html += '</select>';
            return html;
        },
        _text: function (ele, id) {
            var html = '<input type="text" value="' + (ele.default || '') + '" name="' + id + '" id="' + id + '" autocomplete="off" placeholder="请输入.." class="layui-input" ' + ((ele.readonly || '') && 'readonly') + '>'
            return html;
        },
        _numberbox: function (ele, id) {
            var html = '<input type="text" value="' + (ele.default || 0) + '" name="' + id + '" id="' + id + '" lay-verify="number" autocomplete="off" placeholder="请输入.." class="layui-input" ' + ((ele.readonly || '') && 'readonly') + '>'
            return html;
        }
    };
    
    exports('formRender', formRender);
});